package risk.analysis;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.TreeMap;
import java.util.TreeSet;


/**
 * @author Ryu Seung Wan
 *
 */
public class ComparatorUtil {
	
	
	/**
	 * <p>단어 목록을 받아서 단어를 기준으로 정렬(asc) 및 중복을 제거한 목록을 반환한다.</p>
	 * 
	 * @author Ryu Seung Wan
	 * @since 
	 * @version
	 */
	public ArrayList<WordInDoc> wordComparator(ArrayList<WordInDoc> wordList){
		TreeSet<WordInDoc> ts = new TreeSet<WordInDoc>(new WordComparator());
		ArrayList<WordInDoc> tempWordList = new ArrayList<WordInDoc>();
		
		for(WordInDoc wid : wordList){
			ts.add(wid);
		}

		tempWordList.addAll(ts);
		
		return tempWordList;
	}

	/**
	 * <p>단어 목록을 받아서 단어 일련번호를 기준으로 정렬(asc)</p>
	 * 
	 * @author Ryu Seung Wan
	 * @since 
	 * @version
	 */
	public ArrayList<WordInDoc> wordSeqComparator(ArrayList<WordInDoc> wordList){
		TreeSet<WordInDoc> ts = new TreeSet<WordInDoc>(new WordSeqComparator());
		ArrayList<WordInDoc> tempWordList = new ArrayList<WordInDoc>();
		
		for(WordInDoc wid : wordList){
			ts.add(wid);
		}

		tempWordList.addAll(ts);
		
		return tempWordList;
	}

	/**
	 * <p>단어 목록을 받아서 단어 시작위치값을 기준으로 정렬(asc)</p>
	 * 
	 * @author Ryu Seung Wan
	 * @since 
	 * @version
	 */
	public ArrayList<WordInDoc> wordPostionComparator(ArrayList<WordInDoc> wordList){
		TreeSet<WordInDoc> ts = new TreeSet<WordInDoc>(new WordPostionComparator());
		ArrayList<WordInDoc> tempWordList = new ArrayList<WordInDoc>();
		
		for(WordInDoc wid : wordList){
			ts.add(wid);
		}

		tempWordList.addAll(ts);
		
		return tempWordList;
	}

	
	/**
	 * <p>meta 목록을 받아서 문서번호로 정렬하고 중복을 제거한 목록을 반환한다.</p>
	 * 
	 * @author Ryu Seung Wan
	 * @since 
	 * @version
	 */
	public ArrayList<Meta> metaComparator(ArrayList<Meta> docList){
		TreeSet<Meta> ts = new TreeSet<Meta>(new MetaComparator());
		ArrayList<Meta> tempDocList = new ArrayList<Meta>();
		
		for(Meta meta : docList){
			ts.add(meta);
		}

		tempDocList.addAll(ts);
		
		return tempDocList;
	}

	/**
	 * <p>숫자 목록을 받아서  정렬(asc) 및 중복을 제거한 목록을 반환한다.</p>
	 * 
	 * @author Ryu Seung Wan
	 * @since 
	 * @version
	 */
	public ArrayList<Integer> intComparator(ArrayList<Integer> intList){
		TreeSet<Integer> ts = new TreeSet<Integer>(new IntComparator());
		ArrayList<Integer> tempIntList = new ArrayList<Integer>();
		
		for(Integer i : intList){
			ts.add(i);
		}

		tempIntList.addAll(ts);
		
		return tempIntList;
	}
	

	// debugging
	public static void main(String[] agrs){
		
		ArrayList<Integer> intList = new ArrayList<Integer>();
		
		for(int i=39100;i<39103;i++){
			intList.add(i + 1);
		}
		intList.add(39101);
		

		ComparatorUtil cu = new ComparatorUtil();
		intList = cu.intComparator(intList);
		
		for(int i: intList){
			System.out.println(i);
		}
		
	}
}


class IntComparator implements Comparator<Integer>{
	public int compare(Integer o1, Integer o2){ 
		return o1 > o2 ? -1 : (o1 == o2 ? 0 : 1); 
	}
}

class StringComparator implements Comparator<String>{
	public int compare(String o1, String o2){ 
		return o1.compareToIgnoreCase(o2);
	}
}

class WordComparator implements Comparator<WordInDoc>{
	public int compare(WordInDoc o1, WordInDoc o2) {    
		return o1.getWord().compareToIgnoreCase(o2.getWord());
	}
}


class WordSeqComparator implements Comparator<WordInDoc>{
	public int compare(WordInDoc o1, WordInDoc o2) {    
		return o1.getSeq() < o2.getSeq() ? -1 : o1.getSeq() == o2.getSeq() ? 0 : 1;
	}
}

class WordPostionComparator implements Comparator<WordInDoc>{
	public int compare(WordInDoc o1, WordInDoc o2) {    
		return o1.getStart() < o2.getStart() ? -1 : o1.getStart() == o2.getStart() ? 0 : 1;
	}
}

class MetaComparator implements Comparator<Meta>{
	public int compare(Meta o1, Meta o2) {    
		return o1.getMt_no() > o2.getMt_no() ? -1 : o1.getMt_no() == o2.getMt_no() ? 0 : 1;
	}
}


